﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace UsageFileReader
{
    public partial class Details : Form
    {
        
        public Details(SPUsageEntry Entry)
        {
            InitializeComponent();
            if (null !=Entry)
            {
                this.Text = UsageDataTable.GetDisplayNameOfType(Entry.GetType());
                PopulateData(Entry);
            }
        }

        private void SetCellValues(string propText, object valText)
        {
            int newRowIndex = dgvDetails.Rows.Add();
            DataGridViewRow row = dgvDetails.Rows[newRowIndex];
            row.Cells["Property"].Value = propText;
            row.Cells["Value"].Value = valText;
        }

        private void PopulateData(SPUsageEntry Entry)
        {
            if (null !=Entry)
            {
                 SetCellValues("Correlation Id",Entry.CorrelationId);
                 SetCellValues("Farm Id",Entry.FarmId);
                 SetCellValues("Machine Name",Entry.MachineName);
                 SetCellValues("Parent Type",Entry.ParentType);
                 SetCellValues("Parent Type Guid",Entry.ParentTypeGuid);
                 SetCellValues("Site Subscription Id",Entry.SiteSubscriptionId);
                 SetCellValues("Timestamp",Entry.Timestamp);
                 SetCellValues("Timestamp Utc",Entry.TimestampUtc);
                 SetCellValues("User",Entry.User);

                Type eType =Entry.GetType();
                #region SPClickthroughUsageEntry
                if (eType == typeof(SPClickthroughUsageEntry))
                {
                    SPClickthroughUsageEntry typed = (SPClickthroughUsageEntry)Entry;

                    SetCellValues("Browser Session Id", typed.BrowserSessionId);
                    SetCellValues("Clicked Asset ContentType", typed.ClickedAssetContentType);
                    SetCellValues("Clicked Asset Id", typed.ClickedAssetId);
                    SetCellValues("Clicked Asset Web Id", typed.ClickedAssetWebId);
                    SetCellValues("Clicked Title", typed.ClickedTitle);
                    SetCellValues("Click Time", typed.ClickTime);
                    SetCellValues("Click Type", typed.ClickType);
                    SetCellValues("Custom Data", typed.CustomData);
                    SetCellValues("I sBest Bet Result", typed.IsBestBetResult);
                    SetCellValues("Origin", typed.Origin);
                    SetCellValues("Origin Web Id", typed.OriginWebId);
                    SetCellValues("Query Id", typed.QueryId);
                    SetCellValues("Result Position", typed.ResultPosition);
                    SetCellValues("Sample Rate", typed.SampleRate);
                    SetCellValues("Site Id", typed.SiteId);
                    SetCellValues("User Department", typed.UserDepartment);
                    SetCellValues("User Id", typed.UserId);
                    SetCellValues("User Title", typed.UserTitle);
                }
                #endregion

                #region SPFeatureUsageEntry
                else if (eType == typeof(SPFeatureUsageEntry))
                {
                    SPFeatureUsageEntry typed = (SPFeatureUsageEntry)Entry;
                    SetCellValues("Feature Id", typed.FeatureId);
                    SetCellValues("Site Url", typed.SiteUrl);


                }
                #endregion

                #region SPQueryUsageEntry
                else if (eType == typeof(SPQueryUsageEntry))
                {
                    SPQueryUsageEntry typed = (SPQueryUsageEntry)Entry;
                    SetCellValues("Browser Culture", typed.BrowserCulture);
                    SetCellValues("Browser Session Id", typed.BrowserSessionId);
                    SetCellValues("Contextual Scope Name", typed.ContextualScopeName);
                    SetCellValues("Contextual Scope Url", typed.ContextualScopeUrl);
                    SetCellValues("Custom Data", typed.CustomData);
                    SetCellValues("Did You Mean Invoked", typed.DidYouMeanInvoked);
                    SetCellValues("Machine Name", typed.MachineName);
                    SetCellValues("Num Best Bets", typed.NumBestBets);
                    SetCellValues("Origin", typed.Origin);
                    SetCellValues("Query Id", typed.QueryId);
                    SetCellValues("Query String", typed.QueryString);
                    SetCellValues("Query String Language", typed.QueryStringLanguage);
                    SetCellValues("Query Time", typed.QueryTime);
                    SetCellValues("Query Type", typed.QueryType);
                    SetCellValues("Sample Rate", typed.SampleRate);
                    SetCellValues("Scope Name", typed.ScopeName);
                    SetCellValues("Search Svc App Id", typed.SearchSvcAppId);
                    SetCellValues("Search Svc Farm Id", typed.SearchSvcFarmId);
                    SetCellValues("Total Pages", typed.TotalPages);
                    SetCellValues("Total Results", typed.TotalResults);
                    SetCellValues("User Department", typed.UserDepartment);
                    SetCellValues("User Id", typed.UserId);
                    SetCellValues("User Title", typed.UserTitle);
                    SetCellValues("Web Id", typed.WebId);

                }
                #endregion

                #region SPRatingUsageEntry
                else if (eType == typeof(SPRatingUsageEntry))
                {
                    SPRatingUsageEntry typed = (SPRatingUsageEntry)Entry;
                    SetCellValues("Browser Session Id", typed.BrowserSessionId);
                    SetCellValues("Custom Data", typed.CustomData);
                    SetCellValues("Feedback Answer Id", typed.FeedbackAnswerId);
                    SetCellValues("Feedback Answer Numeric Equivalent", typed.FeedbackAnswerNumericEquivalent);
                    SetCellValues("Feedback Answer Text", typed.FeedbackAnswerText);
                    SetCellValues("Feedback Control Culture", typed.FeedbackControlCulture);
                    SetCellValues("Feedback Identifier", typed.FeedbackIdentifier);
                    SetCellValues("Feedback QuestionId", typed.FeedbackQuestionId);
                    SetCellValues("Feedback Question Text", typed.FeedbackQuestionText);
                    SetCellValues("Feedback Time", typed.FeedbackTime);
                    SetCellValues("Is UserSatisfaction Answer", typed.IsUserSatisfactionAnswer);
                    SetCellValues("Query Id", typed.QueryId);
                    SetCellValues("Rated Asset Id", typed.RatedAssetId);
                    SetCellValues("Rated Asset Title", typed.RatedAssetTitle);
                    SetCellValues("Rated Asset Web Id", typed.RatedAssetWebId);
                    SetCellValues("Sample Rate", typed.SampleRate);
                    SetCellValues("Site Id", typed.SiteId);
                    SetCellValues("User Department", typed.UserDepartment);
                    SetCellValues("User Id", typed.UserId);
                    SetCellValues("User Title", typed.UserTitle);
                    SetCellValues("User Verbatim", typed.UserVerbatim);

                }
                #endregion

                #region SPRequestUsageEntry
                else if (eType == typeof(SPRequestUsageEntry))
                {
                    SPRequestUsageEntry typed = (SPRequestUsageEntry)Entry;
                    SetCellValues("Browser", typed.Browser);
                    SetCellValues("Bytes Sent", typed.BytesSent);
                    SetCellValues("Content Type Id", typed.ContentTypeId);
                    SetCellValues("Doc Name And Path", typed.DocNameAndPath);
                    SetCellValues("Duration", typed.Duration);
                    SetCellValues("Http Status", typed.HttpStatus);
                    SetCellValues("Operation Count", typed.OperationCount);
                    SetCellValues("Query Count", typed.QueryCount);
                    SetCellValues("Query Duration Sum", typed.QueryDurationSum);
                    SetCellValues("Query String", typed.QueryString);
                    SetCellValues("Referrer Query String", typed.ReferrerQueryString);
                    SetCellValues("Referrer Url", typed.ReferrerUrl);
                    SetCellValues("Request Count", typed.RequestCount);
                    SetCellValues("Request Type", typed.RequestType);
                    SetCellValues("Server Url", typed.ServerUrl);
                    SetCellValues("Service Call Count", typed.ServiceCallCount);
                    SetCellValues("Service Call Duration Sum", typed.ServiceCallDurationSum);
                    SetCellValues("Session Id", typed.SessionId);
                    SetCellValues("Site Id", typed.SiteId);
                    SetCellValues("Site Url", typed.SiteUrl);
                    SetCellValues("Title", typed.Title);
                    SetCellValues("User Address", typed.UserAddress);
                    SetCellValues("User Agent", typed.UserAgent);
                    SetCellValues("Web Application Id", typed.WebApplicationId);
                    SetCellValues("Web Id", typed.WebId);
                    SetCellValues("Web Url", typed.WebUrl);
                }
                #endregion

                #region SPSiteInventoryUsageEntry
                else if (eType == typeof(SPSiteInventoryUsageEntry))
                {
                    SPSiteInventoryUsageEntry typed = (SPSiteInventoryUsageEntry)Entry;
                    SetCellValues("Max Storage", typed.MaxStorage);
                    SetCellValues("Parent Web Id", typed.ParentWebId);
                    SetCellValues("Server Url", typed.ServerUrl);
                    SetCellValues("Site Id", typed.SiteId);
                    SetCellValues("Site Size", typed.SiteSize);
                    SetCellValues("Site Template", typed.SiteTemplate);
                    SetCellValues("Site Url", typed.SiteUrl);
                    SetCellValues("Web Application Id", typed.WebApplicationId);
                    SetCellValues("Web Id", typed.WebId);
                    SetCellValues("Web Url", typed.WebUrl);

                }
                #endregion

                #region SPTimerJobUsageEntry
                else if (eType == typeof(SPTimerJobUsageEntry))
                {
                    SPTimerJobUsageEntry typed = (SPTimerJobUsageEntry)Entry;

                    SetCellValues("Start Time", typed.StartTime);
                    SetCellValues("End Time", typed.EndTime);
                    SetCellValues("Duration", typed.Duration);
                    SetCellValues("Job Id", typed.JobId);
                    SetCellValues("Job Title", typed.JobTitle);
                    SetCellValues("Query Count", typed.QueryCount);
                    SetCellValues("Query Duration Sum", typed.QueryDurationSum);
                    SetCellValues("Request Count", typed.RequestCount);
                    SetCellValues("Server Id", typed.ServerId);
                    SetCellValues("Service Call Count", typed.ServiceCallCount);
                    SetCellValues("Service Call Duration Sum", typed.ServiceCallDurationSum);
                    SetCellValues("Service Id", typed.ServiceId);
                    SetCellValues("Status", typed.Status);
                    SetCellValues("Web Application Id", typed.WebApplicationId);
                    SetCellValues("Web Application Name", typed.WebApplicationName);

                }
                #endregion
            }
        }


    }
}
